package com.ly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.entity.Member;
import com.ly.entity.Student;
import com.ly.entity.User;
import com.ly.mapper.MemberMapper;
import com.ly.mapper.StudentMapper;
import com.ly.mapper.UserMapper;
import com.ly.pojo.dto.StudentDTO;
import com.ly.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : 黑渊白花
 * @ClassName StudentServiceImpl
 * @date : 2025/2/8 22:31
 * @Description
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MemberMapper memberMapper;


    @Override
    public IPage<Student> getPage(StudentDTO studentDTO) {
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(studentDTO.getName()),Student::getName,studentDTO.getName());
        lqw.like(Strings.isNotEmpty(studentDTO.getCollege()),Student::getCollege,studentDTO.getCollege());;
        IPage page = new Page(studentDTO.getPageNum(),studentDTO.getPageSize());
        studentMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public Student getByUserId(Integer id) {
        log.info("{id}");
        QueryWrapper qw=new QueryWrapper();
        qw.eq("user_id",id);
        Student student = studentMapper.selectOne(qw);
        return student;
    }

    @Override
    public Boolean isDelete(Student student) {
        if(student==null){
            return false;
        }
        User user = userMapper.selectById(student.getUserId());
        //是社长不能删除
        if(user.getType()=="1"){
            return false;
        }
        LambdaQueryWrapper<Member> lqw = new LambdaQueryWrapper<>();

        lqw.like(Strings.isNotEmpty(Integer.toString(student.getId())),Member::getStuId,student.getId());
        Long count = memberMapper.selectCount(lqw);
        if(count>0){
            return false;
        }
        studentMapper.deleteById(student);
        userMapper.deleteById(user);
        return true;
    }
}
